package com.sangmo.fts.process.factoring.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.common.utils.RandomUtils;
import com.sangmo.fts.process.factoring.interfaces.FfWaybillCreation;
import com.sangmo.fts.process.factoring.model.FfContract;
import com.sangmo.fts.process.factoring.model.FfContractItem;
import com.sangmo.fts.process.factoring.model.FfWaybill;
import com.sangmo.fts.process.factoring.model.FfWaybillItem;
import com.sangmo.fts.process.factoring.repo.FfWaybillRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class FfWaybillEntityService extends AuthorityEntityService<FfWaybill> {

    @Autowired
    private FfWaybillRepo ffWaybillRepo;

    @Autowired
    private FfContractService ffContractService;

    @Autowired
    private FfWaybillItemEntityService ffWaybillItemEntityService;

    @Override
    protected IDataViewRepo<FfWaybill> getRepo() {
        return ffWaybillRepo;
    }

    @Override
    protected void onCreate(FfWaybill entity, Object bo) {
        super.onCreate(entity, bo);
        entity.setCode(RandomUtils.generateRandomNum(12));
        FfWaybillCreation creation = (FfWaybillCreation) bo;
        FfContract contract = ffContractService.get(creation.getContractId());
        entity.setShipper(contract.getSellerId());
        entity.setConsignee(contract.getBuyerId());
        entity.setContractAmount(ffContractService.calculateContractAmount(creation.getContractId()));
    }

    @Override
    protected void afterCreated(FfWaybill entity, Object bo) {
        super.afterCreated(entity, bo);
        List<FfContractItem> contractItems = ffContractService.listItemsByContractId(entity.getContractId());
        List<FfWaybillItem> waybillItems = buildWaybillItems(entity.getId(), contractItems);
        ffWaybillItemEntityService.saveAll(waybillItems);
    }

    private List<FfWaybillItem> buildWaybillItems(Long waybillId, List<FfContractItem> contractItems) {
        List<FfWaybillItem> waybillItems = new ArrayList<>();
        for (FfContractItem contractItem : contractItems) {
            FfWaybillItem item = FfWaybillItem.fromContractItem(contractItem);
            item.setWaybillId(waybillId);
            waybillItems.add(item);
        }
        return waybillItems;
    }

    @Override
    protected void onDelete(FfWaybill entity) {
        super.onDelete(entity);
        if (!FfWaybill.States.PENDING.equals(entity.getState())) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ffWaybill.remove.permit.disabled");
        }
    }

    public FfWaybill findByContractId(Long contractId) {
        return ffWaybillRepo.findFirstByContractId(contractId);
    }
}
